import Vue from 'vue'

Vue.filter('toFixed2',(num, place)=>{
    return num ? num.toFixed(place || 2) : '0.00'
})

Vue.filter('dateFormat',(ms,day,str='-', hasYear=true)=>{
    if(!ms){
        return "";
    }
    ms = Number(ms);
    let _date = new Date(ms);
    let year=_date.getFullYear(),
        month=_date.getMonth()+1,
        date=_date.getDate(),
        hour=_date.getHours(),
        minute=_date.getMinutes(),
        second=_date.getSeconds();
    return (hasYear ? (year+str) : '') +(month<10 ? ("0"+month) : month)+str+(date<10 ? ("0"+date) : date)+(!day ? (" "+(hour<10 ? ("0"+hour) : hour)+":"+(minute<10?("0"+minute):minute)+":"+(second<10?("0"+second):second)) : ""); 
})

Vue.filter('getTime',(time,ms)=>{

    if(!time){
        return "";
    }
    time = Number(time);
    var _date = new Date(time);
    var hour=_date.getHours(),
        minute=_date.getMinutes(),
        second=_date.getSeconds();
        
    return (hour<10 ? ("0"+hour) : hour)+":"+(minute<10?("0"+minute):minute) + (ms ? (":"+(second<10?("0"+second):second)) : '' ); 
})

Vue.filter('placeholderImg',(url)=>{

    return url ? url : 'http://www.lunzi.online/static/img/logo.cc58945.png';
})

Vue.filter('getCloseDate', dateNum => {
    let minute = 1000 * 60,
        hour = minute * 60,
        day = hour * 24,
        halfamonth = day * 15,
        month = day * 30;

    let now = new Date().getTime(),
        diffValue = now - dateNum,
        monthC = diffValue / month,
        weekC = diffValue / (7 * day),
        dayC = diffValue / day,
        hourC = diffValue / hour,
        minC = diffValue / minute,
        result = "";

    if (monthC >= 1) {
        result = parseInt(monthC) + "个月前";
    } else if (weekC >= 1) {
        result = parseInt(weekC) + "周前";
    } else if (dayC >= 1) {
        result = parseInt(dayC) + "天前";
    } else if (hourC >= 1) {
        result = parseInt(hourC) + "个小时前";
    } else if (minC >= 1) {
        result = parseInt(minC) + "分钟前";
    } else
        result = "刚刚";

    return result;
})
